package com.company.project.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.company.project.common.utils.JwtUtil;
import com.company.project.common.utils.RestUtil;
import com.company.project.pojo.Rest;
import com.company.project.pojo.ServiceCode;
import com.company.project.pojo.institution.Institution;
import com.company.project.pojo.institution.InstitutionDTO;
import com.company.project.pojo.roleVillage.RoleVillageDTO;
import com.company.project.pojo.village.Village;
import com.company.project.pojo.village.VillageDTO;
import com.company.project.service.IInstitutionService;
import com.company.project.service.IRoleVillageService;
import com.company.project.service.IVillageService;
import io.jsonwebtoken.Claims;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 任建波
 * @since 2019-08-17
 */
@Api(value = "角色村庄关联",tags = "角色村庄关联")
@Controller
@RequestMapping("roleVillage")
public class RoleVillageController {

    @Autowired
    private IInstitutionService iInstitutionService;
    @Autowired
    private IVillageService iVillageService;
    @Autowired
    private IRoleVillageService iRoleVillageService;
    @Autowired
    HttpServletRequest request;

    @ApiOperation(value = "获取管理区村庄列表")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="header", name = "token", value = "token", required = true, dataType = "String"),
            @ApiImplicitParam(paramType="query", value = "角色id", name = "roleId", required = true, dataType = "int")
    })
    @RequestMapping(value = "getInstitutionVillageListByRoleId", method = RequestMethod.GET)
    @ResponseBody
    public Rest getInstitutionVillageListByRoleId(Integer roleId){
        // 根据角色id查询村庄
        List<RoleVillageDTO> roleVillageDTOList = iRoleVillageService.getRoleVillageListByRoleId(roleId);

        // 查询管理区
        QueryWrapper<Institution> institutionQueryWrapper = new QueryWrapper<>();
        institutionQueryWrapper.eq("type", 2);
        institutionQueryWrapper.eq("is_delete", 0);
        List<Institution> institutionList = iInstitutionService.list(institutionQueryWrapper);

        Integer[] institutionIds = new Integer[institutionList.size()];
        for (int i=0; i<institutionList.size(); i++){
            institutionIds[i] = institutionList.get(i).getId();
        }

        // 根据管理区ids查询村庄
        QueryWrapper<Village> villageQueryWrapper = new QueryWrapper<>();
        villageQueryWrapper.eq("is_delete", 0);
        villageQueryWrapper.in("institution_id", institutionIds);
        List<Village> villageList = iVillageService.list(villageQueryWrapper);

        List<InstitutionDTO> institutionDTOList = new ArrayList<>();
        for (Institution institution: institutionList){
            InstitutionDTO institutionDTO = new InstitutionDTO();
            BeanUtils.copyProperties(institution, institutionDTO);
            institutionDTO.setCheckedInstitution(false);    // 默认不选中管理区

            // 管理区的村庄
            List<VillageDTO> villageArrayList = new ArrayList<>();
            for (Village village: villageList){
                VillageDTO villageDTO = new VillageDTO();
                BeanUtils.copyProperties(village, villageDTO);
                villageDTO.setScope(1); // 默认所有数据
                //
                if(villageDTO.getInstitutionId().equals(institution.getId())){
                    // 回显范围
                    for (RoleVillageDTO roleVillageDTOS: roleVillageDTOList){
                        if(roleVillageDTOS.getVillageId().equals(villageDTO.getId())){
                            villageDTO.setScope(roleVillageDTOS.getScope());
                            institutionDTO.setCheckedInstitution(true); // 选中管理区
                        }
                    }
                    villageArrayList.add(villageDTO);
                }
            }

            // 角色管理区的村庄
            List<Integer> checkedVillageIds = new ArrayList<>();
            for (int i=0; i<roleVillageDTOList.size(); i++){
                if(roleVillageDTOList.get(i).getInstitutionId().equals(institution.getId())){
                    checkedVillageIds.add(roleVillageDTOList.get(i).getVillageId());
                }
            }

            institutionDTO.setVillageDTOList(villageArrayList);
            institutionDTO.setCheckedVillageIds(checkedVillageIds);
            institutionDTOList.add(institutionDTO);
        }

        return RestUtil.rest(ServiceCode.CODE_200, "成功！！！" , institutionDTOList);
    }

}

